package com.hj.student;

import java.util.ArrayList;
import java.util.Scanner;

public class Function {
    public static void change(ArrayList<Student> list) {
        System.out.println("请输入您要修改的学号:");
        String updateSid = new Scanner(System.in).nextLine();
        int index = getIndex(list, updateSid);
        if(index == -1){
            System.out.println("该信息不存在, 请重新输入");
        }else{
            System.out.println("请输入新的姓名:");
            String name = new Scanner(System.in).nextLine();
            System.out.println("请输入新的年龄:");
            int age = new Scanner(System.in).nextInt();
            System.out.println("请输入新的生日:");
            String birthday = new Scanner(System.in).nextLine();
            Student stu = new Student(updateSid,name,age,birthday);
            list.set(index, stu);
            System.out.println("修改成功!");
        }
        System.out.println();
    }

    public static void delete(ArrayList<Student> list) {
        System.out.println("请输入您要删除的学号:");
        String delSid = new Scanner(System.in).nextLine();
        int index = getIndex(list, delSid);
        if(index == -1){
            System.out.println("该信息不存在, 请重新输入");
        }else{
            list.remove(index);
            System.out.println("删除成功!");
        }
        System.out.println();
    }

    public static int getIndex(ArrayList<Student> list, String sid){
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if(sid.equals(stu.getSid())){
                index = i;
            }
        }
        return index;
    }

    public static void finAll(ArrayList<Student> list) {
        if (list == null || list.size() == 0) {
            System.out.println("无学生信息，请添加后重试");
        } else {
            System.out.println("学号\t\t姓名\t年龄\t生日");
            for (Student stu : list) {
                System.out.println(stu.getSid() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t\t" + stu.getBirthday());
            }
        }
        System.out.println();
    }

    public static void add(ArrayList<Student> list) {
        String sid;
        while(true){
            System.out.println("请输入学号:");
            sid = new Scanner(System.in).nextLine();
            int index = getIndex(list, sid);
            if(index == -1){
                break;
            }else{
                System.out.println("学号已被占用");
                System.out.println();
            }
        }
        System.out.println("请输入姓名:");
        String name = new Scanner(System.in).nextLine();
        System.out.println("请输入年龄:");
        int age = new Scanner(System.in).nextInt();
        System.out.println("请输入生日:");
        String birthday = new Scanner(System.in).nextLine();
        Student stu = new Student(sid, name, age, birthday);
        list.add(stu);
        System.out.println("添加学生成功");
        System.out.println();
    }
}
